package com.example.demo.oo.controller;

import com.example.demo.app.Response;
import com.example.demo.oo.domain.Captcha;
import com.example.demo.oo.domain.User;
import com.example.demo.oo.service.CaptchaService;
import com.example.demo.oo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;

    @Autowired
    CaptchaService captchaService;

    @PostMapping("login")
    public Response login(@Valid Captcha captcha, User user) {


        // Controller用法：从HTTP请求中获得信息，提取参数，并分发给不同的处理服务

        // 验证码是否正确
        //captchaService.check(captcha);

        user = userService.loginOrRegister(user);

        return success(user);
    }

    @PostMapping("recharge")
    public Response recharge() {

        User user = new User();
        user.setId(1);

        int money = 10;

        userService.recharge(user, money);

        return success();
    }
}
